package com.wingy.test;

import android.util.Xml;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class ConfigFlie {

    public boolean getISNIGHT() {
        return ISNIGHT;
    }

    public String getPWD() {
        return PWD;
    }

    private boolean ISNIGHT;
    private String PWD;

    public ConfigFlie(boolean isnight, String pwd) {
        this.ISNIGHT = isnight;
        this.PWD = pwd;
    }

    public void setISNIGHT(boolean isnight) {
        this.ISNIGHT = isnight;
    }
    public  void setPWD(String pwd) {
        this.PWD = pwd;
    }

    ConfigFlie mconfigFlie;


    //保存配置文件
    public void Saveconfig(String path, ConfigFlie configFlie1) {

        mconfigFlie = new ConfigFlie(true, "");
        mconfigFlie = configFlie1;

        try {
            File file = new File(path,"config.xml");
            FileOutputStream fos = new FileOutputStream(file);
            // 获得一个序列化工具
            XmlSerializer serializer = Xml.newSerializer();
            serializer.setOutput(fos, "utf-8");
            // 设置文件头
            serializer.startDocument("utf-8", true);
            serializer.startTag(null, "config");

            // 写日夜主题
            serializer.startTag(null, "ISNIGHT");
            serializer.text(String.valueOf(mconfigFlie.getISNIGHT()));
            serializer.endTag(null, "ISNIGHT");
            //写秘钥
            serializer.startTag(null, "pwd");
            serializer.text(mconfigFlie.getPWD());
            serializer.endTag(null, "pwd");


            serializer.endTag(null, "config");
            serializer.endDocument();
            fos.close();
            //Toast.makeText(MainActivity.this, "写入成功", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
            //Toast.makeText(MainActivity.this, "写入失败", Toast.LENGTH_SHORT).show();
        }

    }

    //读入配置文件
    public ConfigFlie Readconfig(String path) {
        ConfigFlie temp = new ConfigFlie(true,"");
        mconfigFlie = temp;
        try {
            File newpath = new File(path, "config.xml");
            FileInputStream fis = new FileInputStream(newpath);

            // 获得pull解析器对象
            XmlPullParser parser = Xml.newPullParser();
            // 指定解析的文件和编码格式
            parser.setInput(fis, "utf-8");

            int eventType = parser.getEventType(); // 获得事件类型

            while (eventType != XmlPullParser.END_DOCUMENT) {
                String tagName = parser.getName(); // 获得当前节点的名称

                switch (eventType) {
                    case XmlPullParser.START_TAG: // 当前等于开始节点
                        if ("config".equals(tagName)) {
                        } else if ("ISNIGHT".equals(tagName)) {
                            mconfigFlie.setISNIGHT( Boolean.parseBoolean(parser.nextText()));
                        }else if ("pwd".equals(tagName)) {
                            mconfigFlie.setPWD(parser.nextText());
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        break;
                    default:
                        break;
                }
                eventType = parser.next(); // 获得下一个事件类型
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return mconfigFlie;
    }
}
